package book.action;

import static common.utils.Constants.LIMIT;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import service.impl.BookServiceImpl;

import common.action.BaseAction;
import common.pojo.Book;
import common.utils.Constants;
import common.utils.Page;
import common.utils.PageUtil;
import common.utils.PaginationUtil;
import common.utils.SessionUtils;

@Scope("prototype")
@Controller(value = "book")
public class BookAction extends BaseAction {

	
	/**
	 * 获取列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getBookListByPage() throws Exception {
		String currentTmp = request.getParameter("current");//获取页码参数
		Book b = new Book();
		b.setDeleted(Constants.NOT_DELETED);
		int total= bService.queryByCount(b);//查询符合条件的类型数量
		int current = PageUtil.getCurrent(currentTmp);//判断当前页码数
		Page page = PageUtil.createPage(LIMIT, total, current);//生成页码实体参数
		blist = bService.find(page.getBeginIndex(), LIMIT);//根据页码参数以及每页显示数量获取列表
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"getBookListByPage.action","");//页码显示的静态代码，字符串，很长
		return "bList";
	}


	/**
	 * 删除
	 */
	public String deleteById() {
		String ids = request.getParameter("ids");
		bService.delete(ids,SessionUtils.getAdminName(request));
		return SUCCESS;
	}
	
	/**
	 * 增加
	 * @return
	 */
	public String add() {
		String price=request.getParameter("price");
		String dprice=request.getParameter("dprice");
		bService.add(book,price,dprice);
		if(StringUtils.isBlank(dofunction))dofunction="()";//刷新页面
		return SUCCESS;
	}
	
	/**
	 * 更新
	 * @return
	 */
	public String update() {
		String price=request.getParameter("price");
		String dprice=request.getParameter("dprice");
		book.setUpdateName(SessionUtils.getAdminName(request));
		book.setUpdateTime(new Date());
		bService.update(book,price,dprice);
		if(StringUtils.isBlank(dofunction))dofunction="()";//刷新页面
		return SUCCESS;
	}
	
	/**
	 * 按id查询
	 * @return
	 */
	public String findInfoById() {
		String id = request.getParameter("id");
		book=bService.findInfoById(id);
		return "info";
	}
	private static final long serialVersionUID = 1L;

	//引用service层
	@Resource
	private BookServiceImpl bService;
	private Book book;
	private List<Book> blist;//列表
	private String pagination;//分页
	private String dofunction;//返回处理刷新

	
	


	public Book getBook() {
		return book;
	}


	public void setBook(Book book) {
		this.book = book;
	}


	public List<Book> getBlist() {
		return blist;
	}


	public void setBlist(List<Book> blist) {
		this.blist = blist;
	}


	public String getDofunction() {
		return dofunction;
	}

	public void setDofunction(String dofunction) {
		this.dofunction = dofunction;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}



}
